{% import "_includes/forms" as forms %}

{% if sections %}

    {% if craft.app.getIsMultiSite() %}
        {% set editableSites = craft.app.sites.getEditableSites() %}

        {% if editableSites|length > 1 %}
            {% set siteInput %}
                <div class="select">
                    <select id="site-id" name="siteId">
                        {% for site in editableSites %}
                            <option value="{{ site.id }}"{% if site.id == widget.siteId %} selected{% endif %}>{{ site.name|t('site') }}</option>
                        {% endfor %}
                    </select>
                </div>
            {% endset %}

            {{ forms.field({
                id: 'site-id',
                label: "Site"|t('app'),
            }, siteInput) }}
        {% endif %}
    {% endif %}

    {% set sectionOptions = [] %}
    {% for section in sections %}
        {% set sectionOptions = sectionOptions|merge([{ label: section.name|t('site'), value: section.id }]) %}
    {% endfor %}
    {{ forms.selectField({
        label: "Section"|t('app'),
        instructions: "Which section do you want to publish entries to?"|t('app'),
        id: 'section',
        name: 'section',
        options: sectionOptions,
        value: widget.section,
        toggle: true,
        targetPrefix: 'section'
    }) }}

    {% set selectedSectionId = widget.section %}

    {% for section in sections %}
        {% set showSection = ((not selectedSectionId and loop.first) or selectedSectionId == section.id) %}
        <div id="section{{ section.id }}"{% if not showSection %} class="hidden"{% endif %}>

            {% set entryTypeOptions = [] %}
            {% for entryType in section.getEntryTypes() %}
                {% set entryTypeOptions = entryTypeOptions|merge([{ label: entryType.name|t('site'), value: entryType.id }]) %}
            {% endfor %}

            {% if entryTypeOptions|length == 1 %}
                {{ hiddenInput("sections[#{section.id}][entryType]", widget.entryType) }}
            {% else %}
                {{ forms.selectField({
                    label: "Entry Type"|t('app'),
                    instructions: "Which type of entries do you want to publish?"|t('app'),
                    id: 'entryType',
                    name: 'sections['~section.id~'][entryType]',
                    options: entryTypeOptions,
                    value: widget.entryType,
                    toggle: true,
                    targetPrefix: 'section'~section.id~'-type'
                }) }}
            {% endif %}

            {% set fieldsInput %}
                {% set entryTypes = section.getEntryTypes() %}
                {% for entryType in entryTypes %}
                    {% set showEntryType = (((not showSection or not widget.entryType) and loop.first) or widget.entryType == entryType.id or entryTypes|length == 1) %}
                    <div id="section{{ section.id }}-type{{ entryType.id }}"{% if not showEntryType %} class="hidden"{% endif %}>
                        {% for field in entryType.getFieldLayout().getFields() %}
                            <div>
                                {{ forms.checkbox({
                                    label: raw(field.name|e ~ (field.required ? '<span class="required"></span>')),
                                    name: 'sections['~section.id~'][fields][]',
                                    value: field.id,
                                    checked: (field.required or field.id in widget.fields),
                                    disabled: field.required
                                }) }}
                            </div>
                        {% endfor %}
                    </div>
                {% endfor %}
            {% endset %}

            {{ forms.field({
                label: "Fields"|t('app'),
                instructions: "Which fields should be visible in the widget?"|t('app')
            }, fieldsInput) }}
        </div>
    {% endfor %}

{% else %}

    <p>{{ "No sections are available."|t('app') }}</p>

{% endif %}
